package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        try (
                Connection connection=DBUtil.getConnection();
                ){
            Statement statement=connection.createStatement();
            String sql="SELECT t.name,t.gender,c.name,c.floor " +
                       "FROM teacher t,class c " +
                       "WHERE t.id=c.teacher_id " +
                       "AND t.name='王克晶'";
            ResultSet rs =statement.executeQuery(sql);
            while (rs.next()){
//                String name=rs.getString("t.name");
//                String gender=rs.getString("t.gender");
//                String className=rs.getString("c.name");
//                String floor=rs.getString("c.floor");
                String name=rs.getString(1);//获取结果集的第一个字段 注意区别Java的下标
                //注意；下标从1开始
                String gender=rs.getString(2);
                String className=rs.getString(3);
                String floor=rs.getString(4);
                System.out.println(name + ", " + gender + ", " + className + ", " + floor);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
